home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / games / nhak_src.zip / PCUNIX.C < prev    next >
C/C++ Source or Header  |  1993-03-16  |  5KB  |  206 lines

  1. /*    SCCS Id: @(#)pcunix.c    3.0    89/12/29
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. /* This file collects some Unix dependencies; pager.c contains some more */
  6.  
  7. /*
  8.  * The time is used for:
  9.  *    - seed for rand()
  10.  *    - year on tombstone and yymmdd in record file
  11.  *    - phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
  12.  *    - night and midnight (the undead are dangerous at midnight)
  13.  *    - determination of what files are "very old"
  14.  */
  15.  
  16. #include "hack.h"    /* mainly for index() which depends on BSD */
  17.  
  18. #ifndef MACOS
  19. #include    <sys/types.h>
  20. #include    <sys/stat.h>
  21. #endif
  22.  
  23. static struct tm * NDECL(getlt);
  24.  
  25. #ifdef OVLB
  26.  
  27. #ifndef MACOS
  28. static struct stat buf;
  29. # ifdef WANT_GETHDATE
  30. static struct stat hbuf;
  31. # endif
  32. #endif
  33.  
  34. void
  35. setrandom()
  36. {
  37.     (void) Srand((int) time ((time_t *) 0));
  38. }
  39.  
  40. static struct tm *
  41. getlt()
  42. {
  43.     time_t date;
  44.  
  45.     (void) time(&date);
  46.     return(localtime(&date));
  47. }
  48.  
  49. int
  50. getyear()
  51. {
  52.     return(1900 + getlt()->tm_year);
  53. }
  54.  
  55. char *
  56. getdate()
  57. {
  58.     static char datestr[7];
  59.     register struct tm *lt = getlt();
  60.  
  61.     Sprintf(datestr, "%2d%2d%2d",
  62.         lt->tm_year, lt->tm_mon + 1, lt->tm_mday);
  63.     if(datestr[2] == ' ') datestr[2] = '0';
  64.     if(datestr[4] == ' ') datestr[4] = '0';
  65.     return(datestr);
  66. }
  67.  
  68. int
  69. phase_of_the_moon()            /* 0-7, with 0: new, 4: full */
  70. {                    /* moon period: 29.5306 days */
  71.                     /* year: 365.2422 days */
  72.     register struct tm *lt = getlt();
  73.     register int epact, diy, golden;
  74.  
  75.     diy = lt->tm_yday;
  76.     golden = (lt->tm_year % 19) + 1;
  77.     epact = (11 * golden + 18) % 30;
  78.     if ((epact == 25 && golden > 11) || epact == 24)
  79.         epact++;
  80.  
  81.     return( (((((diy + epact) * 6) + 11) % 177) / 22) & 7 );
  82. }
  83.  
  84. int
  85. night()
  86. {
  87.     register int hour = getlt()->tm_hour;
  88.  
  89.     return(hour < 6 || hour > 21);
  90. }
  91.  
  92. int
  93. midnight()
  94. {
  95.     return(getlt()->tm_hour == 0);
  96. }
  97.  
  98. #ifndef MACOS
  99. void
  100. gethdate(name)
  101. char *name;
  102. {
  103. # ifdef WANT_GETHDATE
  104. /* old version - for people short of space */
  105. /*
  106. /* register char *np;
  107. /*      if(stat(name, &hbuf))
  108. /*          error("Cannot get status of %s.",
  109. /*              (np = rindex(name, '/')) ? np+1 : name);
  110. /*
  111. /* version using PATH from: seismo!gregc@ucsf-cgl.ARPA (Greg Couch) */
  112.  
  113. /*
  114.  * The problem with   #include  <sys/param.h> is that this include file
  115.  * does not exist on all systems, and moreover, that it sometimes includes
  116.  * <sys/types.h> again, so that the compiler sees these typedefs twice.
  117.  */
  118. #define     MAXPATHLEN      1024
  119.  
  120.     register char *np, *path;
  121.     char filename[MAXPATHLEN+1], *getenv();
  122.  
  123.     if (index(name, '/') != NULL || (path = getenv("PATH")) == NULL)
  124.     path = "";
  125.  
  126.     for (;;) {
  127.     if ((np = index(path, ':')) == NULL)
  128.         np = path + strlen(path);       /* point to end str */
  129.     if (np - path <= 1)             /* %% */
  130.         Strcpy(filename, name);
  131.     else {
  132.         (void) strncpy(filename, path, np - path);
  133.         filename[np - path] = '/';
  134.         Strcpy(filename + (np - path) + 1, name);
  135.     }
  136.     if (stat(filename, &hbuf) == 0)
  137.         return;
  138.     if (*np == '\0')
  139.     path = "";
  140.     path = np + 1;
  141.     }
  142.     error("Cannot get status of %s.", (np = rindex(name, '/')) ? np+1 : name);
  143. # endif /* WANT_GETHDATE */
  144. }
  145.  
  146. int
  147. uptodate(fd)
  148. int fd;
  149. {
  150. # ifdef WANT_GETHDATE
  151.     if(fstat(fd, &buf)) {
  152.     pline("Cannot get status of saved level? ");
  153.     return(0);
  154.     }
  155.     if(buf.st_mtime < hbuf.st_mtime) {
  156.     pline("Saved level is out of date. ");
  157.     return(0);
  158.     }
  159. # else
  160. #  if defined(MSDOS) && !defined(NO_FSTAT)
  161.     if(fstat(fd, &buf)) {
  162.     if(moves > 1) pline("Cannot get status of saved level? ");
  163.     else pline("Cannot get status of saved game");
  164.     return(0);
  165.     } 
  166.     if(comp_times(buf.st_mtime)) { 
  167.     if(moves > 1) pline("Saved level is out of date");
  168.     else pline("Saved game is out of date. ");
  169.     return(0);
  170.     }
  171. #  endif  /* MSDOS /* */
  172. # endif /* WANT_GETHDATE */
  173.     return(1);
  174. }
  175. #endif    /* MACOS /* */
  176.  
  177. void
  178. regularize(s)
  179. /*
  180.  * normalize file name - we don't like .'s, /'s, :'s [Mac], or spaces,
  181.  * and in msdos / OS/2 we really get picky
  182.  */
  183. register char *s;
  184. {
  185.     register char *lp;
  186.  
  187. #ifdef MSDOS
  188.     for (lp = s; *lp; lp++)
  189.         if (*lp <= ' ' || *lp == '"' || (*lp >= '*' && *lp <= ',') ||
  190.             *lp == '.' || *lp == '/' || (*lp >= ':' && *lp <= '?') ||
  191. # ifdef OS2
  192.             *lp == '&' || *lp == '(' || *lp == ')' ||
  193. # endif
  194.             *lp == '|' || *lp >= 127 || (*lp >= '[' && *lp <= ']'))
  195.                         *lp = '_';
  196. #else
  197.     while((lp=index(s, '.')) || (lp=index(s, '/')) || (lp=index(s,' '))
  198. # ifdef MACOS
  199.        || (lp=index(s, ':'))
  200. # endif
  201.         ) *lp = '_';
  202. #endif
  203. }
  204.  
  205. #endif /* OVLB */
  206.